package shuashuami.hb.com.avtivity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.manager.HbAppManager;
import shuashuami.hb.com.manager.agent.UserManager;
import shuashuami.hb.com.share.ShareCUserInfo;

public class WelcomeActivity extends AbActivity {
    private int tag;

    @Override
    protected void initUI() {
        setContentView(R.layout.activity_welcome);
    }

    @Override
    protected void initData() {
        HbAppManager.getInstance().SetClearableCookieJar(getApplication());
        Handler handler = new Handler();
        //当计时结束,跳转至主界面
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                isHaveuser();
            }
        }, 3000);
    }

    private void isHaveuser() {
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        if (shareCUserInfo.getId() == null || shareCUserInfo.getId().equals("")) {
            Intent intent = new Intent(WelcomeActivity.this, HbLoginAct.class);
            startActivity(intent);
            WelcomeActivity.this.finish();
        } else {
            UserManager.getInstance().getUser().setId(shareCUserInfo.getId());
            tag = Integer.valueOf(shareCUserInfo.getTag());
            HbAppManager.getInstance().setLoginType(tag);
            if (tag == 1) {
                if (!shareCUserInfo.getwx_sn().equals("") && shareCUserInfo.getwx_sn() != null) {
                    weiChartLogin(shareCUserInfo.getwx_sn(), shareCUserInfo.getUserAvatar());
                } else {
                    login(shareCUserInfo.getPhoneNum(), shareCUserInfo.getPassword());
                }

            } else {
                login(shareCUserInfo.getPhoneNum(), shareCUserInfo.getPassword());
            }
        }

    }

    @Override
    protected void initListener() {

    }

    private void login(String phone, String passowrd) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(2);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        //显示ProgressDialog
        HttpMethods.login(listener,
                phone, passowrd
        );
    }

    private void weiChartLogin(String wxsn, String headimg) {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                //    openActivityAndCloseThis(HbRegistered2Act.class);

                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };

        HttpMethods.weichartLogin(listener,
                wxsn, headimg
        );
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        shareCUserInfo.setwx_sn(wxsn);
        shareCUserInfo.setUserAvatar(headimg);
    }


    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) { //成功
                            ShareCUserInfo shareCUserInfo = new ShareCUserInfo(WelcomeActivity.this);
                            HbAppManager.getInstance().setLoginType(tag);
                            shareCUserInfo.setTag(tag + "");
                            openActivity(HbMainAct.class);
                            finish();
                        } else {
                            openActivity(HbLoginAct.class);
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(WelcomeActivity.this, "数据解析错误");
                    }
                    break;
                case 2:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 200) { //成功

                            openActivity(HbMainAct.class);
                            finish();
                        } else {//初次登陆
                            openActivity(HbLoginAct.class);
                            finish();
                        }
                    } catch (JSONException e) {
                        ToastUtil.showShort(WelcomeActivity.this, "数据解析错误");
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
